import datetime
import jqdatasdk
import json
import os
import pandas as pd
from Library_WEB import settings
from celery import task, Task
from celery import shared_task
from stocksys import models as industry_models
import time
from stocksys.sys_tasks.logic_function import get_cost_time, DjangoTask, task_begin, update_schedule, task_end,workDay
from stocksys.views.data_logic import get_month_exchange_time, get_day_exchange_time, get_yestoday

@shared_task(base=DjangoTask, name="_1_year_industry_data")
def _1_year_industry_data(id):
    max_schedule = 6
    start_time = task_begin(id)
    for current_schedule in range(max_schedule + 1):
        update_schedule(id, current_schedule, max_schedule)
    task_end(id, start_time)
    return 0


@shared_task(base=DjangoTask, name="_1_mounth_industry_data")
def _1_mounth_industry_data(id):
    # 只有不在工作时间才能更新
    if workDay():
        return 0

    def generate_today_data(industry_code, start_date, now_date, today_path):
        new_data = jqdatasdk.get_price(industry_code, start_date=start_date, end_date=now_date,
                                       frequency='minute',
                                       fields=None,
                                       skip_paused=False, fq='pre')
        new_data['date'] = new_data.index
        new_data.index = [x for x in range(len(new_data))]
        new_data.to_csv(today_path, index=False)
        print("no yestoday data:%s new_data length %s" % (today_path, len(new_data)))

    def save_data(industry_code):
        start_date, now_date = get_month_exchange_time()

        workTime = ['09:00:00', '15:30:00']
        dayOfWeek = datetime.datetime.now().weekday()
        beginWork = datetime.datetime.now().strftime("%Y-%m-%d") + ' ' + workTime[0]
        endWork = datetime.datetime.now().strftime("%Y-%m-%d") + ' ' + workTime[1]
        beginWork = datetime.datetime.strptime(beginWork, "%Y-%m-%d %H:%M:%S")
        endWork = datetime.datetime.strptime(endWork, "%Y-%m-%d %H:%M:%S")

        yestoday = get_yestoday()
        saved_path_template = "%s/data/induatry_data/indutry_%s_%s.csv"
        yestoday_path = saved_path_template % (settings.BASE_DIR, industry_code, yestoday)
        today_path = saved_path_template % (settings.BASE_DIR, industry_code, now_date)
        if (os.path.exists(today_path)) or (os.path.exists(yestoday_path)):
            # 如果今天或昨天的数据都没有 那就新建数据
            if not os.path.exists(today_path):
                print("没有今天的%s" % industry_code)
                # 如果没有今天的数据那就是昨天的有
                temp = pd.read_csv(yestoday_path)
                new_date_data = jqdatasdk.get_price(industry_code, start_date=beginWork, end_date=endWork,
                                                    frequency='minute',
                                                    fields=None,
                                                    skip_paused=False, fq='pre')
                new_date_data['date'] = new_date_data.index
                new_date_data.index = [x for x in range(len(new_date_data))]
                new_data = pd.concat([temp, new_date_data], axis=0)
                # 保存今天的数据
                new_data.index = [x for x in range(len(new_data))]
                new_data.to_csv(today_path, index=False)
                # 删除昨天的数据
                print("remove yestoday data:%s new_data length %s" % (yestoday_path, len(new_data)))
                os.remove(yestoday_path)
            else:
                print("有今天的%s" % industry_code)
            return 0
        else:
            # 今天和昨天的都没有 新建数据
            print("今天和昨天都没有%s" % industry_code)
            generate_today_data(industry_code, start_date, now_date, today_path)

    api_keys = industry_models.APIKeys.objects.all().first()
    jqdatasdk.auth(api_keys.username, api_keys.password)

    start_time = task_begin(id)
    industry_code = industry_models.MarketIndex.objects.all()
    max_schedule = industry_code.count()
    update_schedule(id, 0, max_schedule)
    for current_schedule, industry_code in enumerate(industry_code):
        jqdata_code = "%s.%s" % (industry_code.industry_code, industry_code.exchange_abbreviated_name)
        try:
            save_data(jqdata_code)
        except Exception as e:
            print(e)
            state_info = json.dumps({"state": "error",
                                     "current_schedule": "\"%s\"" % str((current_schedule / max_schedule) * 100),
                                     "info": str(e),
                                     "color": "warring",
                                     })
            update_schedule(id, current_schedule + 1, max_schedule, state_info)
            return 0
        update_schedule(id, current_schedule + 1, max_schedule)
    update_schedule(id, max_schedule, max_schedule)
    task_end(id, start_time)
    jqdatasdk.logout()
    return 0



@shared_task(base=DjangoTask, name="all_industry_data")
def all_industry_data(id):
    max_schedule = 6
    start_time = task_begin(id)
    for current_schedule in range(max_schedule + 1):
        update_schedule(id, current_schedule, max_schedule)
    task_end(id, start_time)
    return 0


@shared_task(base=DjangoTask, name="_10_days_industry_data")
def _10_days_industry_data(id):
    max_schedule = 6
    start_time = task_begin(id)
    for current_schedule in range(max_schedule + 1):
        update_schedule(id, current_schedule, max_schedule)
    task_end(id, start_time)
    return 0


@shared_task(base=DjangoTask, name="update_industry_code")
def update_industry_code(id):
    def create_industry_to_industry(industry_code, industry):
        industry = jqdatasdk.get_industry(industry_code, date=datetime.datetime.now().strftime("%Y-%m-%d"))
        if industry != None:
            for industry_level, industry_info in industry[industry_code].items():
                industry_code = industry_info['industry_code']
                industry_name = industry_info['industry_name']
                try:
                    # 获取股票所属行业
                    industry_model = industry_models.Industry.objects.get(code=str(industry_code))
                    # Industry 可能会出现错误
                    industry_models.IndustryIndustry.objects.create(Indstry=industry, industry=industry_model)
                except Exception as e:
                    print(e)

    def create_industry_code(row):
        # 股票交易所地址
        exchange_address = industry_models.ExchangeAddress.objects.get(
            industry_jqdata_name=row['exchange_abbreviated_name'])
        jqdata_industry_code = row['industry_code'] + "." + row['exchange_abbreviated_name']

        industry = industry_models.Industry.objects.create(
            industry_name=row['industry_name'],
            indutry_api_name=str(row['industry_api_name']),
            industry_code=row['industry_code'],
            last_update_time=row['last_update_time'],
            start_date=row['start_date'].to_pydatetime(),
            end_date=row['end_date'].to_pydatetime(),
            type=row['type'],
            exchange_address=exchange_address,
            exchange_abbreviated_name=row['exchange_abbreviated_name'],
            industry_abbreviated_name=row['industry_abbreviated_name']
        )
        create_industry_to_industry(jqdata_industry_code, industry)

    start_time = task_begin(id)
    data = jqdatasdk.get_all_securities(['industry'])

    data['industry_code'] = data.index
    data.index = [x for x in range(len(data))]
    data['exchange_abbreviated_name'] = data['industry_code'].map(lambda x: x.split('.')[1])
    data['industry_code'] = data['industry_code'].map(lambda x: x.split('.')[0])
    data['industry_api_name'] = data['exchange_abbreviated_name'].map(lambda x: 'sh' if x == 'XSHG' else 'sz')

    data['industry_abbreviated_name'] = data['name']
    data = data.drop('name', axis=1)
    data['industry_name'] = data['display_name']
    data = data.drop('display_name', axis=1)
    data['industry_class'] = data['exchange_abbreviated_name'].map(lambda x: 1 if x == 'XSHG' else 2)
    data['last_update_time'] = datetime.datetime.now().strftime("%Y-%m-%d")
    data = data[
        ['industry_class', 'industry_name', 'industry_abbreviated_name', 'industry_code', 'industry_api_name', 'last_update_time',
         'start_date', 'end_date', 'type', 'exchange_abbreviated_name']]

    print('载入股票代码数据')
    max_schedule = len(data)
    for current_schedule in range(max_schedule):
        row = data.loc[current_schedule].to_dict()
        try:
            industry = industry_models.Industry.objects.get(industry_code=row['industry_code'], industry_name=row['industry_name'])
            industry.last_update_time = datetime.datetime.now()
            industry.save()
        except:
            print('有新股票上线')
            create_industry_code(row)
        update_schedule(id, current_schedule + 1, max_schedule)
    update_schedule(id, max_schedule, max_schedule)
    task_end(id, start_time)
    return 0
